#include "CmdSettingDialogSingleton.h"

CmdSettingDialogSingleton& CmdSettingDialogSingleton::getInstance()
{
	static CmdSettingDialogSingleton instance;
	return instance;
}

void CmdSettingDialogSingleton::show()
{
	cmd_dialog->show();
}

void CmdSettingDialogSingleton::initValue(QString name, QString data_f, QByteArray data)
{
	old_cmd_data.name = name.toStdString();
	old_cmd_data.data_f = data_f.toStdString();
	old_cmd_data.data = data.toHex().toStdString();

	name_edit->setText(name);
	if (data_f == "text") {
		hup_combo->setCurrentIndex(0);
		text_radio->setChecked(true);
		hex_radio->setChecked(false);

		data_edit->setText(data);
		return;
	}
	else if(data_f == "hex") {
		hup_combo->setCurrentIndex(0);
		text_radio->setChecked(false);
		hex_radio->setChecked(true);

		data_edit->setText(data.toHex());
		return;
	}
	if (data_f == "hup_text") {
		text_radio->setChecked(true);
		hex_radio->setChecked(false);
		hup_cmd_edit->setText(QString(data.toHex().at(4)) + QString(data.toHex().at(5)));
		data_edit->setText(data.mid(HUP_FRAME_PREFIX_NUMBER, data.size() - HUP_FRAME_PREFIX_NUMBER - 1));
	}
	else if (data_f == "hup_hex") {
		text_radio->setChecked(false);
		hex_radio->setChecked(true);
		hup_cmd_edit->setText(QString(data.toHex().at(4))+ QString(data.toHex().at(5)));
		data_edit->setText(data.mid(HUP_FRAME_PREFIX_NUMBER, data.size() - HUP_FRAME_PREFIX_NUMBER - 1).toHex());
	}

	if (data.at(0) == HUP_FRAME_HOST_HDR_H && data.at(1) == HUP_FRAME_HOST_HDR_L) {
		hup_combo->setCurrentIndex(1);
	}
	else {
		hup_combo->setCurrentIndex(2);
	}


}

CmdSettingDialogSingleton::CmdSettingDialogSingleton()
{
	cmd_dialog = new QDialog();
	QVBoxLayout* global_layout = new QVBoxLayout(cmd_dialog);

	QHBoxLayout* name_layout = new QHBoxLayout(cmd_dialog);
	name_layout->addWidget(new QLabel("name:", cmd_dialog));
	name_edit = new QLineEdit(cmd_dialog);
	name_layout->addWidget(name_edit);

	QHBoxLayout* hup_layout = new QHBoxLayout(cmd_dialog);
	hup_layout->addWidget(new QLabel("hup:", cmd_dialog));
	hup_combo = new QComboBox(cmd_dialog);
	hup_combo->addItem("no hup");
	hup_combo->addItem("master");
	hup_combo->addItem("slave");
	hup_layout->addWidget(hup_combo);

	QWidget* hup_cmd_widget = new QWidget(cmd_dialog);
	QHBoxLayout* hup_cmd_layout = new QHBoxLayout(hup_cmd_widget);
	QLabel* hup_cmd_label = new QLabel("hup cmd:");
	hup_cmd_layout->addWidget(hup_cmd_label);
	hup_cmd_edit = new QLineEdit();
	hup_cmd_layout->addWidget(hup_cmd_edit);
	hup_cmd_widget->setLayout(hup_cmd_layout);

	QHBoxLayout* data_choose_layout = new QHBoxLayout(cmd_dialog);
	text_radio = new QRadioButton("text", cmd_dialog);
	hex_radio = new QRadioButton("hex", cmd_dialog);
	text_radio->setChecked(true);
	data_choose_layout->addWidget(text_radio);
	data_choose_layout->addWidget(hex_radio);

	QHBoxLayout* data_layout = new QHBoxLayout(cmd_dialog);
	QLabel* data_format = new QLabel(cmd_dialog);
	data_format->setText("text data:");
	data_edit = new QLineEdit(cmd_dialog);
	QObject::connect(text_radio, &QRadioButton::toggled, cmd_dialog, [=]() {
		data_edit->clear();
		if (text_radio->isChecked()) {
			data_format->setText("text data:");
		}
		else {
			data_format->setText("hex data:");
		}
		});
	data_layout->addWidget(data_format);
	data_layout->addWidget(data_edit);

	QHBoxLayout* op_layout = new QHBoxLayout(cmd_dialog);
	sure = new QPushButton("sure", cmd_dialog);
	cancel = new QPushButton("cancel", cmd_dialog);
	connect(sure, &QPushButton::clicked, cmd_dialog, [=]() {
		if (name_edit->text().isEmpty()) {
			QMessageBox::information(cmd_dialog, "value error", "name cant be empty");
			return;
		}
		name = name_edit->text();
		QString data_f = "text";
		QByteArray data;
		if (hup_combo->currentIndex() == 0) {
			if (text_radio->isChecked()) {
				data_f = "text";
				data = data_edit->text().toLatin1();
			}
			else {
				data_f = "hex";
				data = QByteArray::fromHex(data_edit->text().remove(' ').toLatin1());
			}
		}
		else if (hup_combo->currentIndex() == 1) {
			if (text_radio->isChecked()) {
				data_f = "hup_text";
				data = data_edit->text().toLatin1();
				data = HupProtocal::encode(EM_HUP_ROLE_MASTER, hup_cmd_edit->text().toUInt(nullptr, 0), data);
			}
			else {
				data_f = "hup_hex";
				data = QByteArray::fromHex(data_edit->text().remove(' ').toLatin1());
				data = HupProtocal::encode(EM_HUP_ROLE_MASTER, hup_cmd_edit->text().toUInt(nullptr, 0), data);
			}

		}
		else if (hup_combo->currentIndex() == 2) {
			if (text_radio->isChecked()) {
				data_f = "hup_text";
				data = data_edit->text().toLatin1();
				data = HupProtocal::encode(EM_HUP_ROLE_SLAVE, hup_cmd_edit->text().toUInt(nullptr, 0), data);
			}
			else {
				data_f = "hup_hex";
				data = QByteArray::fromHex(data_edit->text().remove(' ').toLatin1());
				data = HupProtocal::encode(EM_HUP_ROLE_SLAVE, hup_cmd_edit->text().toUInt(nullptr, 0), data);
			}
		}
		Cmd_Data cmd_data;
		cmd_data.name = name.toStdString();
		cmd_data.data_f = data_f.toStdString();
		cmd_data.data = data.toHex().toStdString();
		CmdJsonManager::deleteCmd(old_cmd_data);
		CmdJsonManager::addCmd(cmd_data);
		CmdJsonManager::dump();

		emit cmd_setting_finished_signal(name_edit->text(), data_f, data);
		cmd_dialog->hide();
	});
	connect(cancel, &QPushButton::clicked, cmd_dialog, [=]() {
		cmd_dialog->hide();
	});
	op_layout->addWidget(sure);
	op_layout->addWidget(cancel);

	global_layout->addLayout(name_layout);
	global_layout->addLayout(hup_layout);
	global_layout->addWidget(hup_cmd_widget);
	hup_cmd_widget->hide();
	global_layout->addLayout(data_choose_layout);
	global_layout->addLayout(data_layout);
	global_layout->addLayout(op_layout);

	void (QComboBox:: * indexChanged)(int) = &QComboBox::currentIndexChanged;
	connect(hup_combo, indexChanged, this, [=]() {
		if (hup_combo->currentIndex() == 0) {
			hup_cmd_widget->hide();
		}
		else {
			hup_cmd_widget->show();
		}
		});

	cmd_dialog->setLayout(global_layout);
}
